<template>
  <no-ssr>
  <div class="home page-component">
    <el-carousel indicator-position="outside">
      <el-carousel-item v-for="item in 2" :key="item">
        <img src="~assets/images/web-banner1.png" alt="">
      </el-carousel-item>
    </el-carousel>

    <!-- 搜索框 -->
    <div class="search-container">
      <div class="search-wrapper">
        <div class="hospital-search">
          <el-autocomplete
          class="search-input"
          prefix-icon="el-icon-search"
          v-model="state"
          :trigger-on-focus=false
          :fetch-suggestions="querySearchAsync"
          placeholder="点击输入医院名称"
          @select="handleSelect"
          >
            <span slot="suffix" class="search-btn v-link highlight clickable selected">搜索 </span>
          </el-autocomplete>
        </div>
      </div>
    </div>

    <!-- 下方内容 -->
    <div class="bottom">
      <!-- 左下方内容 -->
      <div class="left">
        <!-- 数据字典列表 -->
        <div class="home-filter-wrapper">
          <div class="title"> 医院</div>
          <div>
            <!-- 医院等级列表 -->
            <div class="filter-wrapper">
              <span class="label">等级：</span>
              <div class="condition-wrapper">
                <!-- 医院等级的每一项 -->
                <span class="item v-link clickable"
                :class="hostypeActiveIndex == index ? 'selected' : ''" 
                v-for="(item,index) in hostypeList" 
                :key="index" 
                @click="hostypeSelect(item.value,index)">
                {{ item.name }}
                </span>
              </div>
            </div>

            <!-- 地区列表 -->
            <div class="filter-wrapper">
              <span class="label">地区：</span>
              <div class="condition-wrapper">
                <!-- 地区的每一项 -->
                <span class="item v-link clickable"
                :class="provinceActiveIndex == index ? 'selected' : ''" 
                v-for="(item,index) in districtList" 
                :key="index" 
                @click="districtSelect(item.value, index)">
                {{ item.name }} 
                </span>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 医院列表 -->
        <div class="v-scroll-list hospital-list">
          <div class="v-card clickable list-item" v-for="(item,index) in list" :key="index" @click="show(item.hoscode)">
            <div class="">
              <!-- 医院信息项 -->
              <div class="hospital-list-item hos-item" index="0">
                <div class="wrapper">
                  <!-- 医院名称 -->
                  <div class="hospital-title">{{ item.hosname }}</div>
                  <div class="bottom-container">
                      <!-- 医院等级 -->
                      <div class="icon-wrapper">
                        <span class="iconfont"></span>
                            {{ item.param.hosTypeName }}
                      </div>
                      
                      <!-- 放号时间 -->
                      <div class="icon-wrapper">
                        <span class="iconfont"></span>
                          {{ item.bookingRule.releaseTime }}放号
                      </div>
                  </div>
                </div>
                <!-- 图片 -->
                <img :src="'data:image/jpeg;base64,'+item.logoData" :alt="item.hosname" class="hospital-img">
              </div>
            </div>
          </div>
        </div> 
      </div>

      <!-- 右下方内容 -->
      <div class="right">
        <div class="common-dept">
        <div class="header-wrapper">
        <div class="title"> 常见科室</div>
        <div class="all-wrapper"><span>全部</span>
        <span class="iconfont icon"></span>
        </div>
        </div>
        <div class="content-wrapper">
        <span class="item v-link clickable dark">神经内科 </span>
        <span class="item v-link clickable dark">消化内科 </span>
        <span class="item v-link clickable dark">呼吸内科 </span>
        <span class="item v-link clickable dark">内科 </span>
        <span class="item v-link clickable dark">神经外科 </span>
        <span class="item v-link clickable dark">妇科 </span>
        <span class="item v-link clickable dark"> 产科 </span>
        <span class="item v-link clickable dark">儿科 </span>
        </div>
      </div>
      <div class="space">
        <div class="header-wrapper">
        <div class="title-wrapper">
        <div class="icon-wrapper"><span
        class="iconfont title-icon"></span>
        </div>
        <span class="title">平台公告</span>
        </div>
        <div class="all-wrapper">
        <span>全部</span>
        <span class="iconfont icon"></span>
        </div>
        </div>
        <div class="content-wrapper">
        <div class="notice-wrapper">
        <div class="point"></div>
        <span class="notice v-link clickable dark">关于延长北京大学国际医院放假的通知 </span>
        </div>
        <div class="notice-wrapper">
        <div class="point"></div>
        <span class="notice v-link clickable dark">北京中医药大学东方医院部分科室医生门诊医 </span>
        </div>
        <div class="notice-wrapper">
        <div class="point"></div>
        <span class="notice v-link clickable dark"> 武警总医院号源暂停更新通知 </span>
        </div>
        </div>
      </div>
      <div class="suspend-notice-list space">
      <div class="header-wrapper">
      <div class="title-wrapper">
        <div class="icon-wrapper">
        <span class="iconfont title-icon"></span>
        </div>
        <span class="title">停诊公告</span>
        </div>
        <div class="all-wrapper">
        <span>全部</span>
        <span class="iconfont icon"></span>
        </div>
        </div>
        <div class="content-wrapper">
        <div class="notice-wrapper">
        <div class="point"></div>
        <span class="notice v-link clickable dark"> 中国人民解放军总医院第六医学中心(原海军总医院)呼吸内科门诊停诊公告 </span>
        </div>
        <div class="notice-wrapper">
        <div class="point"></div>
      <span class="notice v-link clickable dark"> 首都医科大学附属北京潞河医院老年医学科门诊停诊公告 </span>
      </div>
        <div class="notice-wrapper">
          <div class="point"></div>
          <span class="notice v-link clickable dark">中日友好医院中西医结合心内科门诊停诊公告 </span>
        </div>
      </div>
      </div>
      </div>
    </div>
  </div>
  </no-ssr>
</template>
<script>

import hosp_api from '@/api/hosp'
import dict_api from '@/api/dict'
export default {

  // asyncData  服务端渲染，显示医院列表
  asyncData({ params, error}){
    //调用接口
    return hosp_api.getPageList(1,10,null).then(res => {
      return {
        list: res.data.content,
        pages: res.data.totalPages
      }
    })
  },

  //数据模型
  data(){
    return{
      searchObj: {},//搜索框
      page: 1,//当前页
      limit: 10,//每页记录数
      hosname: '', //医院名称
      hostypeList: [], //医院等级集合
      districtList: [], //地区集合
      hostypeActiveIndex: 0,//选中医院下标
      provinceActiveIndex: 0,//选中地区下标
      state:''
    }
  },

  //钩子函数--页面渲染前
  created(){
    this.init()
  },

  //方法
  methods:{
    // 初始化方法
    init(){
      //查询医院等级数据
      dict_api.getByDictCode('Hostype').then(res => {
        this.hostypeList = [],
        this.hostypeList.push({name:'全部', value:''})
        for(var i = 0;i<res.data.length;i++){
            this.hostypeList.push(res.data[i])
        }
      })

      //查询北京地区数据
      dict_api.getByDictCode('Beijin').then(res => {
        this.districtList = [],
        this.districtList.push({name:"全部", value:""})
        for(var i = 0;i<res.data.length;i++){
            this.districtList.push(res.data[i])
        }
      })
    },

    //查询全部医院列表
    getList(){
        hosp_api.getPageList(this.page,this.limit,this.searchObj).then(res => {
          //封装返回的医院集合到list数据模型
          for(let i in res.data.content){
            this.list.push(res.data.content[i])
          }
          //封装数据到page数据模型
          this.totalPages = res.data.totalPages
        })
    },

    //选择不同等级，显示医院列表
    hostypeSelect(hostype,index){
      //准备数据
      this.list = []
      this.page = 1
      this.hostypeActiveIndex = index
      this.searchObj.hostype = hostype

      //调用方法
      this.getList()
    },

    //选择不同地区，显示不同医院列表
    districtSelect(districtCode,index){
      //准备数据
      this.list = []
      this.page = 1
      this.provinceActiveIndex = index
      this.searchObj.districtCode = districtCode

      //调用方法
      this.getList()
    },

    //在输入框输入值，弹出下拉框，显示相关内容
    querySearchAsync(queryString, cb) {
      this.searchObj = []
      if(queryString == '') return
      hosp_api.getByHosname(queryString).then(response => {
        for (let i = 0, len = response.data.length; i <len; i++) {
          response.data[i].value = response.data[i].hosname
        }
        console.log(response.data)
        cb(response.data)
      })
    },

    //在下拉框选择某一个内容，执行下面方法，跳转到详情页面中
    handleSelect(item) {
      window.location.href = '/hospital/' + item.hoscode
    },

    //点击某个医院名称，跳转到详情页面中
    show(hoscode) {
      window.location.href = '/hospital/' + hoscode
    }
  }


}
</script>